<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 具体页面结构 -->
        <!-- departments-组织架构-部门管理
        图标: <svg-icon icon-class="bank" /> -->

        <!-- 1. 表头 -->
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>{{ company.name }}</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <!-- 2.tree -->
        <el-tree
          :data="list"
          default-expand-all
          @node-click="handleNodeClick"
        >
          <template slot-scope="scope">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">

                <svg-icon
                  v-if="scope.data.children.length !== 0"
                  :icon-class="scope.node.expanded ? 'sub' : 'add'"
                />
                <svg-icon v-else icon-class="angel" />

                <!-- <svg-icon :icon-class="getIconName(scope)" />  -->
                <span @click="test(scope)">{{ scope.data.name }}</span>

              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ scope.data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(scope.data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(scope.data.id)">编辑</el-dropdown-item>
                        <el-dropdown-item
                          v-if="scope.data.children.length===0"

                          @click.native="hDel(scope.data.id)"
                        >删除</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>

      </el-card>
    </div>

    <el-dialog
      :title=" isEdit ? '编辑' : '添加'"
      :visible.sync="showDialog"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      @close="hDialogClose"
    >
      <!-- <DeptDialog id="当前点击这个部门的id" /> -->
      <!-- v-if="showDialog" -->
      <DeptDialog
        ref="deptDialog"
        :is-edit="isEdit"
        :dept-id="curId"
        :origin-list="originList"
        @success="hSuccess"
        @close="hClose"
      />
    </el-dialog>
  </div>

</template>
<script>
import { getDepartment, delDepart } from '@/api/department.js'
import { tranListToTreeData } from '@/utils/index.js'
import DeptDialog from './deptDialog.vue'
export default {
  name: 'Departments',
  components: { DeptDialog },
  data() {
    return {
      isEdit: false, // 当前是否是编辑
      curId: '',
      showDialog: false, // 是否显示弹框
      company: { // 公司信息
        name: '',
        id: ''
      },
      // 依赖一份树形数据
      list: [],
      originList: []
    }
  },
  created() {
    // 发请求获取数据
    this.loadDepartments()
  },
  methods: {
    getIconName(scope) {
      // 运算.....
      return 'user'
    },
    // dialog的close事件
    hDialogClose() {
      // 找到子组件，清空表单
      console.log('dialog的close事件', this.$refs.deptDialog)
      this.$refs.deptDialog.resetForm()
    },
    // 收到用户点击取消
    hClose() {
      this.showDialog = false
    },
    // 用户添加部门成功
    hSuccess() {
      // 1. 关闭弹框
      this.showDialog = false
      // 2. 更新tree
      this.loadDepartments()
    },
    test(node, data) {
      console.log(node, data)
    },
    async loadDepartments() {
      // 调用api，发ajx
      const res = await getDepartment()

      this.company.id = res.data.companyId
      this.company.name = res.data.companyName

      // 删除第一项
      res.data.depts.shift()

      // 简化数据，以便传给子组件做自定义表单校验
      this.originList = res.data.depts.map(item => {
        return {
          code: item.code,
          id: item.id,
          pid: item.pid,
          name: item.name
        }
      })

      // 数组转成树
      this.list = tranListToTreeData(res.data.depts)
    },
    // 用户点了添加
    hAdd(id) {
      this.isEdit = false
      // 保存当前部门的id
      this.curId = id
      // 显示弹框
      this.showDialog = true
    },
    async doDel(id) {
      await delDepart(id)
      // 1. 重新请求
      this.loadDepartments()
      // 2. 提示用户
      this.$message({ type: 'success', message: '删除成功!' })
    },
    hDel(id) {
      this.$confirm('此操作将永久删除该部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.doDel(id)
      }).catch(() => {
        // this.$message({
        //   type: 'info',
        //   message: '已取消删除'
        // })
      })
    },
    // 用户点了编辑
    hEdit(id) {
      this.isEdit = true
      // 保存当前部门的id
      // 此时子组件中的deptId并不是当前的id
      this.curId = id
      console.log('this.curId', this.curId)

      // 显示弹框
      this.showDialog = true

      // 找到子组件，调用它的loadDepartmentById方法
      // 如何在父组件调用子组件的方法？ ref
      this.$nextTick(() => {
        console.log('$nextTick', this.$refs.deptDialog.deptId)
        this.$refs.deptDialog.loadDepartmentById()
      })

      // if (this.$refs.deptDialog) {
      //   console.dir(this.$refs.deptDialog.deptId)
      //   // this.$refs.deptDialog.loadDepartmentById(this.curId)
      // }
    },
    handleNodeClick(data) {
      console.log(data)
    }
  }
}
</script>
